﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using Cos.Core.Module.Controller;
using Cos.Core.Module.Response;
using Cos.Core.Module.Service;
using Cos.Core.Service;

namespace Cos.Module.Security.Admin
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Security;

    using Cos.Core;
    using Cos.Core.Attributes;
    using Cos.Core.Model.Common;
    using Cos.Core.Module.Controller;
    using Cos.Core.Module.Service;
    using Cos.Core.Service;
    using Cos.Module.Configuration;

    using Microsoft.Practices.Unity;

    public class SecurityController : ModuleControllerBase, ISecurityController
    {
        public IUserService UserService { get; set; }
        public ICultureService CultureService { get; set; }

        public SecurityController(IUserService userService, ICultureService cultureService)
        {
            this.UserService = userService;
            this.CultureService = cultureService;
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        [Debug]
        [Transaction]
        public virtual CameleonResult<bool> IsAuthenticated()
        {
            return new CameleonResult<bool>(base.Authenticated, CameleonResultStatus.OK);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        [Transaction]
        public virtual CameleonResult Login(string username, string password)
        {
            if (!SessionService.SignIn(username, password))
            {
                return new CameleonResult(CameleonResultStatus.ERROR) { Message = "error.security.invalidcredentials" };
            }

            return new CameleonResult(CameleonResultStatus.OK);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public CameleonResult Logout()
        {
            if (SessionService.SignOut())
            {
                return new CameleonResult(CameleonResultStatus.OK);
            }

            return new CameleonResult(CameleonResultStatus.ERROR);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        [Transaction]
        public virtual CameleonResult ForgotPassword(string username, string email)
        {
            UserService.ForgotPassword(username, email);

            return new CameleonResult(CameleonResultStatus.OK);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public CameleonResult ChangePassword(string oldPassword, string newPassword)
        {
            UserService.ChangePassword(getUser(), oldPassword, newPassword);

            return new CameleonResult(CameleonResultStatus.OK);
        }

        /**
         * {@inheritDoc}
         */
        //@Override
        public CameleonResult<IEnumerable<ShortCosPluginMsg>> ListAvailableServices()
        {
            var pluginService = ServiceLocator.Get<IModuleService>();

            var plugins = pluginService.GetAvailablePlugins(getUser());

            return new CameleonResult<IEnumerable<ShortCosPluginMsg>>(plugins.Select(p => new ShortCosPluginMsg(p)), CameleonResultStatus.OK);
        }
    }
}
